<template>
  <!-- 布局容器container -->
  <el-container class="homeContainer">
    <!-- 头部 -->
    <el-header>
      <header>
        <img src="../assets/logo.png" alt="" />
        <h1>电商后台管理系统</h1>
      </header>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边导航栏 -->
      <el-aside :width="isCollapse ? 64 + 'px' : 200 + 'px'">
        <div class="toggle-nav" @click="toggleNav">| | |</div>
        <!-- 导航栏容器 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#4091FF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <!-- 1级导航栏 -->
          <el-submenu
            :index="menu.id + ''"
            v-for="menu in menuList"
            :key="menu.id"
          >
            <!-- 1级导航栏容器 -->
            <template slot="title">
              <!-- 导航栏的图标 -->
              <i :class="iconfonts[menu.id]"></i>
              <!-- 导航栏的文本内容 -->
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 2级导航栏 -->
            <el-menu-item
              :index="'/' + mixMenu.path + ''"
              v-for="mixMenu in menu.children"
              :key="mixMenu.id"
              @click="activeToggle('/' + mixMenu.path)"
            >
              <template>
                <i class="el-icon-menu"></i>
                <span>{{ mixMenu.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      iconfonts: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      isCollapse: false,
      active: JSON.parse(sessionStorage.getItem("active")) || "",
    };
  },
  methods: {
    // 退出登录回调
    logout() {
      // 清空浏览器会话存储包括token
      window.sessionStorage.clear();
      // 编程式路由导航到login页
      this.$router.push("login");
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$axios.get("/menus");
      console.log(res);
      if (res.meta.status !== 200) return this.$message.err(mes.meta.msg);
      this.menuList = res.data;
    },
    // 点击后切换折叠与展开侧边栏
    toggleNav() {
      this.isCollapse = !this.isCollapse;
    },
    // 点击后高亮2级菜单
    activeToggle(active) {
      this.active = active;
      sessionStorage.setItem("active", JSON.stringify(active));
    },
  },
  created() {
    this.getMenuList();
  },
};
</script>

<style lang="less" scoped>
.iconfont {
  padding-right: 8px;
}

.homeContainer {
  height: 100%;
  & > .el-header {
    z-index: 1;
    box-shadow: 0 4px 5px -3px black;
    background-color: #31373a;
    display: flex;
    justify-content: space-between;
    align-items: center;
    header {
      display: flex;
      align-items: center;
      gap: 10px;
      img {
        height: 50px;
      }
      h1 {
        font-weight: 400;
        color: white;
      }
    }
    .el-button {
      height: 65%;
    }
  }
  .el-aside {
    display: flex;
    flex-direction: column;
    background-color: #333744;
    transition: all 0.2s;
    z-index: 1;
    box-shadow: 4px 0 5px -3px black;
    .el-menu {
      border-right: none;
      .el-submenu {
        i {
          font-size: 20px;
        }
      }
    }
    .toggle-nav {
      height: 25px;
      background-color: #4c5264;
      color: white;
      display: flex;
      justify-content: center;
      font-size: 14px;
      line-height: 25px;
      transition: all 0.3s;
      &:hover {
        cursor: pointer;
        background-color: rgb(63, 66, 82);
      }
    }
  }
  .el-main {
    z-index: 0;
    background-color: #e2e6ea;
  }
}
</style>